export default class AddCanvasGolbal {
  constructor(data) {
    this.viewer = data.viewer;
    this.id = data.id;
    this.position = data.position;
    this.title = data.title;
    this.count = data.count;
    this.imgUrl = data.imgUrl;
    this.width = data.width;
    this.height = data.height;
    this.divIcon = data.divIcon;
    this.ifShowFont = data.ifShowFont;
    this.labelEntity = null;
    //添加画布
    const canvas = document.createElement("canvas");
    canvas.id = "earth-pop" + this.id;
    canvas.width = this.width;
    canvas.height = this.height;
    const ctx = canvas.getContext("2d");

    let divIcon = document.getElementById(this.divIcon);
    ctx.drawImage(divIcon, 0, 0);
    if (this.ifShowFont) {
      ctx.font = "bold 14px SourceHanSansCN-Normal";
      ctx.fillStyle = "#fff";
      ctx.textAlign = "center";
      ctx.fillText(this.title, this.width / 2, 15);

      ctx.font = "bold 16px SourceHanSansCN-Normal";
      ctx.fillStyle = "#fff";
      ctx.textAlign = "center";
      ctx.fillText(this.count + "个", this.width / 2, 35);
    }
    // 将画布转化成图片
    let image = new Image();
    image.src = canvas.toDataURL("image/png");
    const labelEntity = new Cesium.Entity({
      id: this.id,
      position: Cesium.Cartesian3.fromDegrees(
        this.position[0],
        this.position[1]
      ),
      billboard: {
        image: image,
        scale: 1,
        rotation: 0, //指定围绕对齐轴的旋转。
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, //贴地
        disableDepthTestDistance: Number.POSITIVE_INFINITY //不会应用深度测试
      }
    });
    this.labelEntity = labelEntity;
    this.viewer.entities.add(labelEntity);
  }
}
